% clc
% clear 
% clear variables
%% read joint data from random reach experiment
% load random_reach_various_speed_1_5_nosmooth_small_acceleration_torque.mat
% %smooth data 
% for i = 1:size(train_y,2)
%        train_y_smooth(:,i) = smooth(train_y(:,i),11); 
% end
% row_number = size(train_y_smooth,1);
% train_y_smooth = train_y_smooth(1:(row_number-1),:);
% 
% train_x = train_x(1:(row_number-1),:);
% train_y = train_y_smooth;
% 
% for i = 1:size(vali_y,2)
%        vali_y_smooth(:,i) = smooth(vali_y(:,i),11); 
% end
% row_number = size(vali_y_smooth,1);
% vali_y_smooth = vali_y_smooth(1:(row_number-1),:);
% 
% vali_x = vali_x(1:(row_number-1),:);
% vali_y = vali_y_smooth;
%% autoencoder train
[ae_output,features] = AutoEncoder_train(train_x');
%% autoencoder output as NN input
model_AE_NN = NN_train(features,train_y');
%% autoencoder+NN vali
errors_AE_NN_train = NN_vali(model_AE_NN, features,train_y');
features_vali = encode(ae_output,vali_x');
errors_AE_NN = NN_vali(model_AE_NN, features_vali,vali_y');
% %% NN train
% model_NN = NN_train(train_x',train_y');
% %% NN vali
% errors_NN_train = NN_vali(model_NN, train_x',train_y');
% errors_NN = NN_vali(model_NN, vali_x',vali_y');
%% compare error of nn and nn+autoencoder
[nmse_NN_train, rmse_NN_train] = compute_NMSE_RMSE(errors_NN_train, train_y);
[nmse_AE_NN_train, rmse_AE_NN_train] = compute_NMSE_RMSE(errors_AE_NN_train, train_y);
[nmse_NN_vali, rmse_NN_vali] = compute_NMSE_RMSE(errors_NN, vali_y);
[nmse_AE_NN_vali, rmse_AE_NN_vali] = compute_NMSE_RMSE(errors_AE_NN, vali_y);

dof = 6;
nmse = [];
rmse = [];
for i = 1:6
    nmse_temp = [nmse_NN_train(i).^2,nmse_AE_NN_train(i).^2,nmse_NN_vali(i).^2,nmse_AE_NN_vali(i).^2];
    rmse_temp = [rmse_NN_train(i),rmse_AE_NN_train(i),rmse_NN_vali(i),rmse_AE_NN_vali(i)];
    nmse = [nmse; nmse_temp];
    rmse = [rmse; rmse_temp];
end

leg = {['NN_train'],['AE_NN_train'],['NN_vali'],['AE_NN_vali']};
gpname = {['1'],['2'],['3'],['4'],['5'],['6']};
save_figure = true;
folder_name = 'random_various_speed_20160922/';

figure;
fig_nmse = barweb(nmse, zeros(size(nmse)), 1, gpname, '', 'Degree of freedom', 'NMSE', jet,...
'none', leg, 2);
figure;
fig_rmse = barweb(rmse, zeros(size(rmse)), 1, gpname, '', 'Degree of freedom', 'RMSE', jet,...
'none', leg, 2);